package com.example.smsquiz;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity {

	TimePicker tpFrom;
	TimePicker tpTo;
	TimePicker tpFromSec;
	TimePicker tpToSec;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);		
		tpFrom = (TimePicker) findViewById(R.id.tpIntervalFrom);
		tpFrom.setIs24HourView(true);
		tpFrom.setCurrentHour(0);
		tpFrom.setCurrentMinute(0);
		tpTo = (TimePicker) findViewById(R.id.tpIntervalTo);
		tpTo.setIs24HourView(true);
		tpTo.setCurrentHour(0);
		tpTo.setCurrentMinute(0);
		tpFromSec = (TimePicker) findViewById(R.id.tpIntervalFrom_Sec);
		tpFromSec.setIs24HourView(true);
		tpFromSec.setCurrentHour(0);
		tpFromSec.setCurrentMinute(0);
		tpToSec = (TimePicker) findViewById(R.id.tpIntervalTo_Sec);
		tpToSec.setIs24HourView(true);
		tpToSec.setCurrentHour(0);
		tpToSec.setCurrentMinute(10);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);		
		return true;
	}
	
	public void onClickStart(View v) {
		try{
			TextView tbNumber = (TextView) findViewById(R.id.editTextSendMessage);
			String phoneNumber = tbNumber.getText().toString();
			TextView tbStartNumber = (TextView) findViewById(R.id.editStartNumber);
			String startNumber = tbStartNumber.getText().toString();
			if(phoneNumber != null 
					&& phoneNumber.length() > 0
					&& startNumber != null
					&& startNumber.length() > 0){					
				stopService(new Intent(this, SmsQuizService.class));
				Intent serviceIntent = new Intent(this, SmsQuizService.class);
				serviceIntent = serviceIntent.putExtra(SmsQuizService.PHONE_NUMBER_KEY, phoneNumber);
				serviceIntent = serviceIntent.putExtra(SmsQuizService.START_PHONE_NUMBER_KEY, startNumber);
				
				TextView tbTarget = (TextView) findViewById(R.id.editTarget);
				String target = tbTarget.getText().toString();
				serviceIntent = serviceIntent.putExtra(SmsQuizService.TARGET_KEY, Integer.parseInt(target));
				
				TextView tbStartWord = (TextView) findViewById(R.id.editStartWord);
				String startWord = tbStartWord.getText().toString();
				serviceIntent = serviceIntent.putExtra(SmsQuizService.START_WORD_KEY, startWord);
				
				int secFrom = tpFrom.getCurrentHour()*3600 + tpFrom.getCurrentMinute()*60 + tpFromSec.getCurrentMinute();
				serviceIntent.putExtra(SmsQuizService.ANSWER_TIME_FROM_KEY, secFrom);
				
				int secTo = tpTo.getCurrentHour()*3600 + tpTo.getCurrentMinute()*60 + tpToSec.getCurrentMinute();
				serviceIntent.putExtra(SmsQuizService.ANSWER_TIME_TO_KEY, secTo);
				
				startService(serviceIntent);
			}
			else{
				AppLogger.Write("One of phone numbers is null or empty");
			}
		}
		catch (Exception e) {
			AppLogger.WriteError(e);
		}
	}
	
	public void onClickStop(View v) {
		try{
			stopService(new Intent(this, SmsQuizService.class));
		}
		catch(Exception e){
			AppLogger.WriteError(e);
		}
	}

    public void onClickSetAnswer(View v) {
        try{
            startActivity(new Intent(this, SetAnswerActivity.class));
        }
        catch(Exception e){
            AppLogger.WriteError(e);
        }
    }
}
